using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Security.AccessControl;
using System.Security.Principal;

namespace KodeIT.Web.Registration
{
    class Program
    {
        internal const string ISAPI_DLL = "filter_isapi.dll";
        internal const string ISAPI_NAME = "Filter.NET_v1.0.1";

        static void Usage()
        {
            string exeName = Path.GetFileName(Process.GetCurrentProcess().Modules[0].FileName);

            Console.WriteLine("Administration utility (1.0.1) to install and uninstall Filter.NET on the local machine.");
            Console.WriteLine();
            Console.WriteLine("Usage: " + exeName + " -i [-s <path>] | -u ");
            Console.WriteLine();
            Console.WriteLine("-i            Install Filter.NET at global or site scope");
            Console.WriteLine();
            Console.WriteLine("-s <ID>       Install Filter.NET at the specified site ID.");
            Console.WriteLine();
            Console.WriteLine("-u            Uninstall Filter.NET.");
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            int ixArg = 0;
            int argCount = args.Length;
            bool installIsapi = false;
            bool globalScope = true;
            int siteId = 0;

            if( 0 == argCount )
            {
                Usage();
                return;
            }

            //
	        // Get install [-i] or uninstall [-u] option
	        //

	        if( ixArg >= argCount )
	        {
		        Usage();
		        Console.WriteLine("Too few arguments.\r\n");
		        return;
	        }

            if( !args[ixArg].ToLower().Equals("-i") && !args[ixArg].ToLower().Equals("-u") )
            {
                Usage();
		        Console.WriteLine("Wrong option specified. Please use [-i] or [-u] to install or uninstall Filter.NET.\r\n");
		        return;
            }

	        installIsapi = args[ixArg].ToLower().Equals("-i");
            ixArg++;

            //
            // If installing, check the site ID
            //

            if (installIsapi)
            {
                //
                // Get metabase path
                //

                if (ixArg < argCount)
                {
                    globalScope = false;

                    if (!args[ixArg].ToLower().Equals("-s"))
                    {
                        Usage();
                        Console.WriteLine("Wrong option specified. Please use [-s] for a site ID.\r\n");
                        return;
                    }

                    ixArg++;

                    string siteIdAsString = args[ixArg];
                    if (!Int32.TryParse(siteIdAsString, out siteId))
                    {
                        Console.WriteLine("Wrong option specified. When using the [-s] option, the site ID must numeric.");
                    }

                    ixArg++;
                }
            }

	        //
	        // Check end of arguments
	        //

	        if( ixArg < argCount )
	        {
		        Usage();
                Console.WriteLine("Too many arguments\r\n");
		        return;
	        }

            if (installIsapi)
            {
                string currentDirectory = Directory.GetCurrentDirectory();

                Ntfs.AddDirectoryACL("IIS_WPG", currentDirectory);
                Ntfs.AddDirectoryACL("ASPNET", currentDirectory);

                if (globalScope)
                {
                    Iis.AddFilter(
                        ISAPI_NAME,
                        Path.Combine(currentDirectory, ISAPI_DLL));
                }
                else
                {
                    Iis.AddFilter(
                        siteId, 
                        ISAPI_NAME,
                        Path.Combine(currentDirectory, ISAPI_DLL));
                }
            }
            else
            {
                Iis.RemoveFilter(ISAPI_NAME, true);
            }

            Iis.StopIis();
            Iis.StartIis();
        }
    }
}
